package Exercise;

//罗马数字的转换------方法二
import java.util.Scanner;
public class String_7 {
    public static void main(String[] args) {
        //用户输入
        Scanner sc = new Scanner(System.in);
        String sr;
        while(true){
            System.out.println("请输入一个长度小于等于9，并且全部为数字的字符串:");
            sr = sc.next();
            //合法性检验
            if(Judge(sr))
                break;
        }
        //转变罗马数字
        System.out.println(Trans(sr));
    }

    //检验
    public static boolean Judge(String sr){
        //长度大于九，返回false
        if(sr.length()>9){
            System.out.println("长度错误，请重新输入！");
            return false;
        }
        //内容不为数字，返回false
        for (int i = 0; i < sr.length(); i++) {
            if(sr.charAt(i)<'0'||sr.charAt(i)>'9'){
                System.out.println("内容错误，请重新输入！");
                return false;
            }
        }
        return true;
    }
    //转变
    public static String Trans(String sr){
        StringBuilder sb = new StringBuilder();
        String[] src = {"","I","II","III","IV","V","VI","VII","VII","IX"};
        for (int i = 0; i < sr.length(); i++) {
            int r = sr.charAt(i)-'0';
            sb.append(src[r]).append(" ");
        }
        String s = sb.toString();
        return s;
    }
}

